package com.scoy.honeymei.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.MainActivity;
import com.scoy.honeymei.activity.mall.SearchGoodsActivity;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentAdapter;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentIntAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.CateBean;
import com.scoy.honeymei.bean.LoginBean;
import com.scoy.honeymei.fragment.mall.GoodsListFragment;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.umeng.socialize.utils.ContextUtil.getContext;

/**
 * @ClassName: MallFragment$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/30/0030$ 上午 9:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/30/0030$ 上午 9:20$
 * @UpdateRemark: 更新内容
 */
public class MallFragment extends BaseFragment {

    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.hm_search_tv)
    TextView hmSearchTv;
    @BindView(R.id.hm_tocart_iv)
    ImageView hmTocartIv;
    @BindView(R.id.hm_tab)
    TabLayout hmTab;
    @BindView(R.id.hm_vp)
    ViewPager hmVp;
    private Unbinder unbinder;
    private Context mContext;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mall, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();


        initNormal();

        httpCate();

        return view;
    }

    @Override
    public void initNormal() {


        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initTab(List<CateBean.GoodsCategoryBean> goodsCategory) {
        String[] mTitles = new String[goodsCategory.size()];
        //  int[] mTitles = new int[]{R.string.attention, R.string.find, R.string.around, R.string.attention, R.string.find, R.string.around, R.string.attention, R.string.find, R.string.around};
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < goodsCategory.size(); i++) {
            mTitles[i] = goodsCategory.get(i).getName();
            fragments.add(GoodsListFragment.newInstance(i, goodsCategory.get(i).getId()));
        }

        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter( getChildFragmentManager(), fragments, mTitles);
        hmTab.setupWithViewPager(hmVp, false);
        hmVp.setAdapter(tabFragmentAdapter);
        hmVp.setCurrentItem(0);
    }

    @OnClick({R.id.hm_search_tv, R.id.hm_tocart_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.hm_search_tv:
                startActivityForResult(new Intent(mContext, SearchGoodsActivity.class), 0x1);
                break;
            case R.id.hm_tocart_iv:
                if (onOneClick != null) {
                    onOneClick.oneClick(1);
                }
                break;
        }
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }


    //获取分类
    private void httpCate() {
        HttpParams params = new HttpParams();

        HpGo.newInstance().HttpGo(getActivity(), MyUrl.GOODS_CARE, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                CateBean cateBean = new Gson().fromJson((String) o, CateBean.class);
                List<CateBean.GoodsCategoryBean> goodsCategory = cateBean.getGoodsCategory();
                if (goodsCategory != null && goodsCategory.size() > 0) {
                    initTab(goodsCategory);
                }
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }


}
